package com.aiju.cash.ui.discount;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DiscountApi;
import com.aiju.cash.util.InputFilter.AijuInputFilter;
import com.aiju.utils.T;

/**
 * @author: 佐为
 * @date: 2017-05-11 10:41
 * @version: V1.0
 * @Description: 满减创建
 */
public class DiscountFullCreateActivity extends BaseTitleActivity {
    private String type;
    private EditText conditionEditText, moneyEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discount_full_create);
        String title = getIntent().getStringExtra("title");
        setCenterTitle(title);
        type = "新建满减规则".equals(title) ? "3" : "4";
        setRightTitle("保存");
        conditionEditText = (EditText) findViewById(R.id.et_discount_condition);
        moneyEditText = (EditText) findViewById(R.id.et_discount_money);
        if (type.equals("3")) {
            moneyEditText.setFilters(new InputFilter[]{new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    String oriValue = dest.toString();
                    StringBuffer sb = new StringBuffer(oriValue);
                    sb.append(source);

                    String newValue = sb.toString();
                    Double aDouble = Double.valueOf(newValue);
                    if (aDouble > 10000) {
                        T.ToastShort("立减金额不能超过1万");
                        return "";
                    }
                    if (dest.toString().contains(".")) {
                        int index = dest.toString().indexOf(".");
                        int mlength = dest.toString().substring(index).length();
                        if (mlength == 3) {
                            return "";
                        }
                    }
                    return source;
                }
            }});
        } else {
            ((TextView) findViewById(R.id.tv_discount_tip)).setText("立享");
            moneyEditText.setHint("请输入折扣");
            moneyEditText.setFilters(new InputFilter[]{
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                            String oriValue = dest.toString();
                            StringBuffer sb = new StringBuffer(oriValue);
                            sb.append(source);
                            String newValue = sb.toString();

                            Double aDouble = Double.valueOf(newValue);
                            if (aDouble >= 10 || newValue.length() > 3 || aDouble < 1) {
                                return "";
                            }
                            return source;
                        }
                    }
            });
        }
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String conditon = conditionEditText.getText().toString();
                String money = moneyEditText.getText().toString();
                if (TextUtils.isEmpty(conditon)) {
                    T.ToastShort("请输入消费所需达到的金额");
                    return;
                }
                if (type.equals("3")) {
                    if (TextUtils.isEmpty(money)) {
                        T.ToastShort("请输入立减金额");
                        return;
                    }
                    Double d1 = Double.valueOf(conditon);
                    Double d2 = Double.valueOf(money);
                    if (d2 >= d1) {
                        T.ToastShort("立减金额需小于消费金额");
                        return;
                    }
                } else {
                    if (TextUtils.isEmpty(money)) {
                        T.ToastShort("请输入折扣");
                        return;
                    }
                }
                new DiscountApi(mDialogHelper).save(type, conditon, money, new SimpleHttpCallback() {
                    @Override
                    protected void onDataSuccess(String data) {
                        T.ToastShort("创建成功");
                        finish();
                    }
                });
            }
        });

        conditionEditText.setFilters(new InputFilter[]{new AijuInputFilter()});
    }
}
